#include "common.h"
#include "electric_motor.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>

ElectricMotorProperty ElectricMotorProperty_createValue(
    const char name[NAME_LENGTH],
    ELECTRIC_MOTOR_TYPE type,
    double maxPower,
    double maxTorque,
    double maxRPM,
    double efficiency,
    double gearRatio)
{
    ElectricMotorProperty motorProperty;
    memcpy(motorProperty.name, name, NAME_LENGTH);
    motorProperty.maxPower = maxPower;
    motorProperty.maxTorque = maxTorque;
    motorProperty.maxRPM = maxRPM;
    motorProperty.efficiency = efficiency;
    motorProperty.gearRatio = gearRatio;
    motorProperty.type = type;
    return motorProperty;
}

ElectricMotor *ElectricMotor_createMulti(int motorNum)
{
    assert(motorNum > 0);
    ElectricMotor *motors = (ElectricMotor*)malloc(sizeof(ElectricMotor) * motorNum);
    for (int i = 0; i < motorNum; i++)
    {
        ElectricMotor *motor = &(motors[i]);
        motor->id = i;
        motor->status = 1;
    }
    printf("Created multiple motors, head pointer is at %p\n", motors);
    return motors;
}

double ElectricMotorProperty_getMaxPerimeterSpeed(
    ElectricMotorProperty *motorProperty, double wheelDiameter)
{
    double wheelPerimeter = PI * wheelDiameter;
    double wheelMaxRPM = (motorProperty->maxRPM) / (motorProperty->gearRatio);
    return wheelMaxRPM / 60 * wheelPerimeter * 3.6;
}

double ElectricMotorProperty_getTorqueLimitOnAxle(
    ElectricMotorProperty *motorProperty, double wheelDiameter)
{
    return 2 * motorProperty->maxTorque * motorProperty->gearRatio / wheelDiameter / 1000.0;
}